package swe.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import swe.article.Article;
import swe.user.User;
import swe.user.UserDAO;
import swe.user.UserManager;

/**
 * Servlet implementation class Test
 */
@WebServlet("/Rating")
public class RatingServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public RatingServlet() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	
		String action = request.getParameter("action");
		Double rating =Double.parseDouble(request.getParameter("star"));
		
		User user = (User) request.getSession().getAttribute("User");
		Article article = (Article) request.getSession().getAttribute("Article");
		
		UserDAO udao = (UserDAO) getServletContext().getAttribute("udao");
		UserManager um = (UserManager) getServletContext().getAttribute("uManager");
		
		if(action.equals("addRating")) {
				article.addRating(rating, user);
	        	response.setContentType("text/html");
				PrintWriter out = response.getWriter();
				out.println( "<html>" );
				out.println( "<body>" );
				out.println("<h1>Parameter: " + rating + "</h1></html>");
				out.println("<h1>Parameter: " + user + "</h1></html>");
				out.println( "</body>" );
				out.println( "</html>" );
				udao.save(um.getUserList());

			}
			
		}
		
		
	}


